﻿----------------------------------------------------------------------------------------------------
--
--  © 2012 Stanislav S. Yarmonov.
--
--  TODO:LICENSE
--     
----------------------------------------------------------------------------------------------------
EXECUTE [_DB].[Function_Drop] @@Name = N'[_DB].[String_FormatXML]'
GO

----------------------------------------------------------------------------------------------------
--
--	Format string using parameters, described in XML.
--
--	SAMPLE
--		SELECT [dbo].[String_Format](
--			'Welcome %first %last!',
--			(
--				SELECT
--					'Stanislav' AS [first],
--					'Yarmonov' AS [last]
--				FOR XML
--					PATH('')
--			)
--		)
--
----------------------------------------------------------------------------------------------------
CREATE FUNCTION [_DB].[String_FormatXML] (
	@@Format [nvarchar](max),
	@@Params [xml]
)
RETURNS [nvarchar](max)
AS
BEGIN
	DECLARE @Result [nvarchar](max) SET @Result = @@Format
	SELECT
		@Result = REPLACE(
			@Result,
			'%' + [T].[c].value('local-name(.)', 'varchar(128)'),
			[T].[c].value('.[1]', 'nvarchar(max)')
		)
	FROM
		@@Params.nodes('*') AS [T](c)

	RETURN @Result
END
GO
